var CustomerId, CustomerName, CompanyName;
$(function() {
	//初始化iCheck的radio
	$('input[type="radio"]').iCheck({radioClass: 'iradio_square-green'});
	
	// 初始化基本信息页省市区三级联动
	sys.threeLevelArea({
		provinceId: 'province1',
		cityId: 'city1',
		areaId: 'area1',
		streetId: 'street1',
		defaultProvince: $('#province1').attr('init-value'),
		defaultCity: $('#city1').attr('init-value'),
		defaultArea: $('#area1').attr('init-value'),
		defaultStreet: $('#street1').attr('init-value')
	});
	$('#province1').on('changed.bs.select', function (e) {
	  	var text = $(this).find("option:selected").text();
	  	$('#provinceName').val(text);
	});
	$('#city1').on('changed.bs.select', function (e) {
	  	var text = $(this).find("option:selected").text();
	  	$('#cityName').val(text);
	});
	$('#area1').on('changed.bs.select', function (e) {
	  	var text = $(this).find("option:selected").text();
	  	$('#areaName').val(text);
	});
	$('#street1').on('changed.bs.select', function (e) {
	  	var text = $(this).find("option:selected").text();
	  	$('#streetName').val(text);
	});
	
	$('#toSelectCustomer').on('click', function(){
		$('#layer_opt_status').val(''); 
		var url = rootPath + '/customer/toSelectCustomerPage';
		var index = showDialogUrl('选择客户', 1000, 520, url, function(index){
			var optStatus = $('#layer_opt_status').val();
			if (optStatus == '1') { 
				$('#customerId').val(CustomerId);
				$('#label_customerName').html(CustomerName);
				$('#label_companyName').html(CompanyName);
			}
			layer.close(index);
		})
		layer.full(index);
	})
	
	$("#btn_save").click(function() {
		$("#deliveryAddressForm").submit();
		return false;
	});
	
	$("#btn_back").click(function() {
		window.location.href = rootPath + "/deliveryAddress/toListDeliveryAddressPage";
	});
	
	$("#deliveryAddressForm").validate({
		rules : {
			customerId : {
				required : true 
			},
			receiverName : {
				required : true,
				minlength : 1,
				maxlength : 20
			},
			mobile : {
				required : true,
				minlength : 1,
				maxlength : 20
			},
			provinceCode : {
				required : true 
			},
			cityCode : {
				required : true 
			},
			areaCode : {
				required : true 
			},
			streetCode : {
				required : true 
			},
			address : {
				required : true,
				minlength : 1,
				maxlength : 200
			},
			isDefault : {
				required : true,
				minlength : 1,
				maxlength : 5
			},
		},
		messages : {
			customerId : {
				required : '请选择客户', 
			},
			receiverName : {
				required : '请填写收货人',
				minlength : '收货人至少3个字符',
				maxlength : '收货人至少20个字符'
			},
			mobile : {
				required : '请填写联系电话',
				minlength : '联系电话至少3个字符',
				maxlength : '联系电话至少20个字符'
			},
			provinceCode : {
				required : '请填写省' 
			},
			cityCode : {
				required : '请填写市' 
			},
			areaCode : {
				required : '请填写区' 
			},
			streetCode : {
				required : '请填写街道' 
			},
			address : {
				required : '请填写详细地址',
				minlength : '详细地址至少3个字符',
				maxlength : '详细地址至少200个字符'
			},
			postCode : {
				required : '请填写邮政编码',
				minlength : '邮政编码至少3个字符',
				maxlength : '邮政编码至少6个字符'
			},
			isDefault : {
				required : '请填写是否默认地址' 
			},
		},
		errorPlacement : function(error, element) { // 错误信息位置设置方法
			var tagName = $(element).is("select");
    		if(!tagName) {
    			error.insertAfter(element.parent());
    		}else {
    			error.insertAfter(element.parent());
    		}
		},
		submitHandler : function(form) {
			$(form).ajaxSubmit({
				type		: 'post',
				timeout		: 5000, 
				success		: function(json){ 
					if (json.resultCode == '1') {
						window.location.href = rootPath + "/deliveryAddress/toListDeliveryAddressPage";
					} else { 
						showErr('新增失败！');
					}
				}
			}); 
		}
	});
	
});

